package controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import modelo.SalaReuniao;
import service.SalaReuniaoService;

@Named
@ViewScoped
public class SalaReuniaoController implements Serializable {

	private static final long serialVersionUID = 6514463406965646577L;
	
	private SalaReuniao salaReuniao;
	private List<SalaReuniao> listSalaReuniao;

	@Inject
	private SalaReuniaoService salaReuniaoService;
	
	public SalaReuniaoController() {

	}
	
	@PostConstruct
	public void init(){	
		pesquisarSalaReunioes();
		salaReuniao = new SalaReuniao();
	}

	public String salvar() {
		try {
			salaReuniaoService.persistir(salaReuniao);
			salaReuniao = new SalaReuniao();
			addMessage("Sala de Reuniao salva!");
		} catch (Exception e) {
			addMessage(e.getMessage());
		}
		
		return "cadastraSalaReuniao";
	}
	
	public String editar() {
		try {
			salaReuniaoService.editar(salaReuniao);
			salaReuniao = new SalaReuniao();
			addMessage("Sala de Reuniao atualizada!");
		} catch (Exception e) {
			addMessage(e.getMessage());
		}
		
		return "cadastraSalaReuniao";
	}
	
	public String excluir(){
		try {
			salaReuniaoService.remover(salaReuniao);
			pesquisarSalaReunioes();
			addMessage("Sala de Reuniao excluida!");
		} catch (Exception e) {
			addMessage(e.getMessage());
		}
		return "consultaSalaReuniao";
	}
	
	private void addMessage(String message) {
		FacesMessage msg = new FacesMessage(message);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public void pesquisarSalaReunioes(){
		setListSalaReuniao(salaReuniaoService.recuperarTodos());
	}

	public SalaReuniao getSalaReuniao() {
		return salaReuniao;
	}

	public void setSalaReuniao(SalaReuniao salaReuniao) {
		this.salaReuniao = salaReuniao;
	}

	public List<SalaReuniao> getListSalaReuniao() {
		return listSalaReuniao;
	}

	public void setListSalaReuniao(List<SalaReuniao> listSalaReuniao) {
		this.listSalaReuniao = listSalaReuniao;
	}

	public SalaReuniaoService getSalaReuniaoService() {
		return salaReuniaoService;
	}

	public void setSalaReuniaoService(SalaReuniaoService salaReuniaoService) {
		this.salaReuniaoService = salaReuniaoService;
	}

}
